<?php

namespace app\adminapi\logic\setting\lang;

use app\common\enum\DefaultEnum;
use app\common\logic\BaseLogic;
use app\common\model\lang\LangType;
use think\facade\Db;

/**
 * 语言逻辑层
 * Class LangLogic
 * @package app\adminapi\logic\setting
 */
class LangLogic extends BaseLogic
{

    /**
     * 获取所有语言列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getLangAll()
    {
        return LangType::where('status', '=', DefaultEnum::SHOW)
            ->field('id,language_name,file_name,is_default')
            ->select()
            ->toArray();
    }

    /**
     * @notes 获取语言列表配置
     * @return array
     */
    public static function getLangConfig()
    {
        $list = self::getLangAll();

        $filterArray = array_filter($list, function ($item) {
            return $item['is_default'] === DefaultEnum::SHOW;
        });
        $defaultLang = reset($filterArray)['file_name'];

        $lang_list = [];
        foreach ($list as $k => $item) {
            $lang_list[$k] = [
                'id' => $item['id'],
                'value' => $item['file_name'],
                'label' => $item['language_name'],
            ];
        }

        return [
            'default_lang' => $defaultLang,
            'lang_list' => $lang_list
        ];
    }


    /**
     * @notes 查看语言详情
     * @param $params
     * @return array
     */
    public static function detail(array $params)
    {
        return LangType::where('id', '=', $params['id'])->findOrEmpty()->toArray();
    }

    /**
     * @notes 添加
     * @param array $params
     * @return bool
     */
    public static function add(array $params)
    {
        Db::startTrans();
        try {

            $info = LangType::create([
                'language_name' => $params['language_name'],
                'file_name' => $params['file_name'],
                'status' => $params['status'] ?? DefaultEnum::HIDE,
                'is_default' => $params['is_default'] ?? DefaultEnum::HIDE,
            ]);

            // 默认开启
            if ($params['is_default'] === DefaultEnum::SHOW){
                self::langChangeDefault([
                    'id'=>$info['id'],
                    'is_default'=>DefaultEnum::SHOW,
                ]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 编辑
     * @param $params
     * @return bool
     */
    public static function edit(array $params)
    {
        Db::startTrans();
        try {
            LangType::where('id', $params['id'])
                ->update([
                    'language_name' => $params['language_name'],
                    'file_name' => $params['file_name'],
                    'status' => $params['status'] ?? DefaultEnum::HIDE,
                    'is_default' => $params['is_default'] ?? DefaultEnum::HIDE,
                ]);

            // 默认开启
            if ($params['is_default'] === DefaultEnum::SHOW){
                self::langChangeDefault([
                    'id'=>$params['id'],
                    'is_default'=>DefaultEnum::SHOW,
                ]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * 状态变动
     * @param array $params
     * @return bool
     */
    public static function langChangeStatus(array $params)
    {
        Db::startTrans();
        try {
            LangType::where('id', '=', $params['id'])->update([
                'status' => $params['status']
            ]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * 设置默认语言
     * @param array $params
     * @return bool
     */
    public static function langChangeDefault(array $params)
    {
        Db::startTrans();
        try {

            // 关闭其他默认语言
            LangType::where('id', '<>', $params['id'])->update(['is_default' => DefaultEnum::HIDE]);

            // 设置默认语言
            LangType::where('id', '=', $params['id'])->update([
                'status' => DefaultEnum::SHOW,
                'is_default' => DefaultEnum::SHOW,
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 删除
     * @param $params
     * @return bool
     */
    public static function delete(array $params)
    {
        Db::startTrans();
        try {
            LangType::destroy($params['id']);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

}